<template>
  <div class="base-form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth">
      <el-row>
        <template v-for="item in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <el-form-item
              v-if="!item.isHidden"
              :label="item.label"
              :style="itemStyle"
            >
              <template
                v-if="item.type === 'input' || item.type === 'password'"
              >
                <!--采用双向绑定-->
                <!-- <el-input
                  :placeholder="item.placeholder"
                  :show-password="item.type === 'password'"
                  v-model="formData[`${item.field}`]"
                /> -->

                <!-- 方式2 不采用双向绑定-->
                <el-input
                  :placeholder="item.placeholder"
                  :show-password="item.type === 'password'"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValuechange($event, item.field)"
                />
              </template>
              <template v-else-if="item.type === 'select'">
                <!-- <el-select
                  :placeholder="item.placeholder"
                  style="width: 100%"
                  v-model="formData[`${item.field}`]"
                > -->
                <el-select
                  :placeholder="item.placeholder"
                  style="width: 100%"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValuechange($event, item.field)"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                  >
                    {{ option.title }}
                  </el-option>
                </el-select>
              </template>
              <template v-else-if="item.type === 'datepicker'">
                <!-- <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  v-model="formData[`${item.field}`]"
                ></el-date-picker> -->
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValuechange($event, item.field)"
                ></el-date-picker>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { IFormItem } from '../types'
export default defineComponent({
  props: {
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: '100px'
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: '10px 20px' })
    },
    colLayout: {
      type: Object,
      default: () => ({
        xs: 24,
        sm: 24,
        md: 12,
        lg: 8,
        xl: 6 //>1920 4个
      })
    },
    modelValue: {
      type: Object,
      required: true
    }
  },

  setup(props, { emit }) {
    //方式1 采用双向绑定
    // const formData = ref({ ...props.modelValue })
    // // const formData = computed({
    // //   get: () => props.modelValue,
    // //   set: (newValue) => {
    // //     emit('update:modelValue', newValue)
    // //   }
    // // })
    // watch(formData, (newValue) => emit('update:modelValue', newValue), {
    //   deep: true
    // })

    //方式2 不采用双向绑定
    const handleValuechange = (value: any, field: string) => {
      //props.modelValue解构后将新值赋值到props.modelValue中
      emit('update:modelValue', { ...props.modelValue, [field]: value })
    }
    return { handleValuechange }
  }
})
</script>

<style lang="less" scoped>
.base-form {
  background: white;
  padding-top: 22px;
}
</style>
